package org.opensourcedea.ui.osdeagui;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import org.opensourcedea.dea.*;

public class OpenDEAProblemAction extends Action implements IWorkbenchAction {
	
	private static final String ID = "org.opensourcedea.ui.osdeagui.OpenDEAProblemAction";
	private static final String[] filterNames = {
		"DEA Problem (*.deap)"
	};
	private static final String[] filterExts = {
		"*.deap"
	};
	private Display display;
	
	//DEBUG ONLY
	IWorkbench wb = PlatformUI.getWorkbench();
	IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
	
	
	public OpenDEAProblemAction() {
		setId(ID);
		
		Display.getDefault().syncExec( new Runnable() {  public void run() { } });
		
	}
	
	
	public void run() {
		
//		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();  
//		String dialogBoxTitle = "Question";  
//		String question = "You are in the separate run() method?";  
//		MessageDialog.openQuestion(shell, dialogBoxTitle, question);
		
	    final Shell shell = new Shell(display);
	    

	    FileDialog dlg = new FileDialog(shell, SWT.OPEN);
	    dlg.setFilterNames(filterNames);
	    dlg.setFilterExtensions(filterExts);
	    
	    try {
		    String fileName = dlg.open();
		    if (fileName != null) {
		    	try {
	                FileInputStream fileIn = new FileInputStream(fileName);
	                ObjectInputStream in = new ObjectInputStream(fileIn);
	                DEAProblem rdeaP = (DEAProblem) in.readObject();
	                System.out.println(rdeaP.getDMUName(0).toString());
	                
	                in.close();
	                fileIn.close();
	            }
	            catch(Exception e) {
	                e.printStackTrace();
	            }
		    }
	    }
	    catch (Exception e) {
			e.printStackTrace();
	    }

	    if(display !=null){
	    	display.dispose();
	    }


	  
		
	}
	


	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
	
	}
